#ifndef     __BYTE_VIEW_H
#define     __BYTE_VIEW_H

#include "../build.h"
#include <string.h>

struct ByteView {

    static constexpr usize_t FLEX_LEN = 1;

    inline ByteView() : buf(nullptr), len(0) {}
    inline ByteView(const uint8_t *b, uint16_t l) : buf(b), len(l) {}

    usize_t encode(uint8_t *buf) {
        buf[0] = len;
        memcpy(buf + 1, this->buf, len);
        return len + 1;
    }

    static usize_t content_len(const uint8_t *buf) {
        return buf[0];
    }

    static ByteView decode(const uint8_t *buf) {
        return ByteView(buf + 1, buf[0]);
    }

    const uint8_t *buf;
    uint16_t len;
};

#endif
